package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.system.entity.Employee;
import org.example.system.entity.User;
import org.example.system.mapper.EmployeeMapper;
import org.example.system.service.EmployeeService;
import org.example.exception.ServiceException;
import org.example.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 员工服务实现
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 按条件分页查询
     * 影响条件有员工名，权限，昵称，手机号
     * @param employee
     * @return
     */
    @Override
    public IPage<Employee> selectPage(Employee employee){
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //条件
        if (StrUtil.isNotEmpty(employee.getUsername())){
            wrapper.like(Employee:: getUsername,employee.getUsername());    //相似的员工名（模糊查询）
        }
        if (StrUtil.isNotEmpty(employee.getNickname())){
            wrapper.like(Employee:: getNickname,employee.getNickname());    //相似的昵称（模糊查询）
        }
        if (employee.getAge() != null){
            wrapper.like(Employee:: getAge,employee.getAge());    //年龄
        }
        if (StrUtil.isNotEmpty(employee.getMobile())){
            wrapper.like(Employee:: getMobile,employee.getMobile());    //相似的手机号（模糊查询）
        }
        if (StrUtil.isNotEmpty(employee.getRole())){
            wrapper.eq(Employee:: getRole,employee.getRole());    //相似的权限（模糊查询）
        }
        if (StrUtil.isNotEmpty(employee.getSex())){
            wrapper.eq(Employee:: getSex,employee.getSex());    //相同的性别
        }

        IPage<Employee> page = new Page<>();
        page.setCurrent(employee.getPageNum());
        page.setSize(employee.getPageSize());
        return employeeMapper.selectPage(page,wrapper);
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @Override
    public int add(Employee employee) {
        //用相同的员工名去查数据库
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());   //相同的employeename
        List<Employee> existList = employeeMapper.selectList(wrapper);
        //如果存在抛出一个自定义异常
        if(null != existList && existList.size() > 0){//证明存在员工名相同的员工名
            throw new ServiceException("员工名已存在");
        }
        employee.setPassword(DigestUtil.bcrypt(employee.getPassword()));
        employee.setUpdateTime(new Date());
        employee.setNote("员工创建成功！");
        return employeeMapper.insert(employee);
    }

    /**
     * 修改
     * @param employee
     * @return
     */
    @Override
    public int update(Employee employee) {
        //用相同的员工名去查数据库
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        List<Employee> existList = employeeMapper.selectList(wrapper);
        //如果存在抛出一个自定义异常
        if(null != existList && existList.size() > 0){//证明存在员工名相同的员工名
            Employee existEmployee = existList.get(0);
            if (!existEmployee.getId().equals(employee.getId())) {
                throw new ServiceException("员工名已存在");
            }
        }
        employee.setUpdateTime(new Date());
        if(employee.getPassword().equals(existList.get(0).getPassword()) && employee.getPassword() != null){
            employee.setPassword(null);
        }else{
            employee.setPassword(DigestUtil.bcrypt(employee.getPassword()));
        }
        employee.setNote("修改员工数据");
        return employeeMapper.updateById(employee);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return employeeMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    @Override
    public Employee getOne(Long id) {
        return employeeMapper.selectById(id);
    }

    /**
     * 根据username获取信息
     * @param username
     * @return
     */
    @Override
    public Employee getOne(String username) {
        //用相同的用户名去查数据库
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,username);
        List<Employee> existList = employeeMapper.selectList(wrapper);
        Employee existUser = existList.get(0);
        return existUser;
    }
}
